﻿Public Class clsPrint
    Dim MyCodeBox As TextBoxPrj.CodeEditor
    Dim PrintWidth As Integer
    Dim PrintHeight As Integer
    Dim PrintFont As Font
    Dim LinesPerPage As Integer
    Dim NeedToPrintLines As Integer
    Dim AlreadyPrintLines As Integer
    Dim orgWidth As Integer = 0
    Dim WithEvents mPrintDoc As System.Drawing.Printing.PrintDocument
    Sub New(ByVal CodeBox As TextBoxPrj.CodeEditor, ByVal _PrintDoc As System.Drawing.Printing.PrintDocument)
        MyCodeBox = CodeBox
        mPrintDoc = _PrintDoc
    End Sub
    ''' <summary>
    ''' 打印预览
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub PrintPreView()
        SetPage()
        Dim mPagePreview As New PrintPreviewDialog
        mPagePreview.Document = mPrintDoc
        mPagePreview.ShowDialog()
        MyCodeBox.MainDocument.UnDoWrapWord()
        If MyCodeBox.WordWrapMode Then
            MyCodeBox.MainDocument.DoWrapWord(orgWidth)
        End If
    End Sub
    ''' <summary>
    ''' 页面设置
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub PageSetup()
        Dim mPatgeSetupDialog As New PageSetupDialog
        With mPatgeSetupDialog
            .Document = mPrintDoc
            .PageSettings = mPrintDoc.DefaultPageSettings
        End With
        If mPatgeSetupDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            mPrintDoc.DefaultPageSettings = mPatgeSetupDialog.PageSettings
        End If
    End Sub
    ''' <summary>
    ''' 打印
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Print()
        SetPage()
        Dim mPrintDialog As New PrintDialog
        mPrintDialog.Document = mPrintDoc
        If mPrintDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            mPrintDoc.Print()
        End If
        MyCodeBox.MainDocument.UnDoWrapWord()
        If MyCodeBox.WordWrapMode Then
            MyCodeBox.MainDocument.DoWrapWord(orgWidth)
        End If
    End Sub
    Private Sub SetPage()
        If MyCodeBox.WordWrapMode Then
            orgWidth = MyCodeBox.MainDocument.WordWrapWidth
            MyCodeBox.MainDocument.UnDoWrapWord()
        End If
        With mPrintDoc.PrinterSettings.DefaultPageSettings
            '1/100 Inch
            PrintWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
            PrintHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
        End With
        PrintFont = MyCodeBox.CodeFont
        'Convert To Inch
        PrintWidth = PrintWidth / 100
        'Convert To Piex
        PrintWidth = PrintWidth * MyCodeBox.CreateGraphics.DpiX
        'Convert To Inch
        PrintHeight = PrintHeight / 100
        'Convert To Piex
        PrintHeight = PrintHeight * MyCodeBox.CreateGraphics.DpiY
        LinesPerPage = Math.Floor(PrintHeight / PrintFont.Height)
        MyCodeBox.MainDocument.DoWrapWord(PrintWidth)
        AlreadyPrintLines = 0
        NeedToPrintLines = MyCodeBox.MainDocument.MaxMemoryLineNumber
    End Sub
    Private Sub PrintDraw(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDoc.PrintPage
        Dim g As Graphics = ev.Graphics
        Dim PosY As Integer = ev.MarginBounds.Top * 100 / MyCodeBox.CreateGraphics.DpiY
        Dim PrintLineInPage As Integer
        Do While AlreadyPrintLines < NeedToPrintLines
            g.DrawString(MyCodeBox.MainDocument.MemoryLines(AlreadyPrintLines).Text, PrintFont, Brushes.Black, ev.MarginBounds.Left, PosY)
            PosY += PrintFont.Height
            PrintLineInPage += 1
            AlreadyPrintLines += 1
            If PrintLineInPage = LinesPerPage Then Exit Do
        Loop
        '判断是否要继续打印
        If AlreadyPrintLines < NeedToPrintLines Then
            ev.HasMorePages = True
        Else
            ev.HasMorePages = False
        End If
    End Sub
End Class
